package com.example.qsindusty.jmm_spline.ReleaseOrder;


import android.text.TextUtils;

import com.example.qsindusty.jmm_spline.api.ApiManager;
import com.example.qsindusty.jmm_spline.api.BaseData;
import com.yixin.ystartlibrary.base.BasePresenter;
import com.yixin.ystartlibrary.base.IBaseView;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


/**
 * Created by Administration on 2018/3/26.
 */

public class ReleasePresenter extends BasePresenter implements Contact.Presenter {
    Contact.View mView ;

    @Override
    public void bindView(IBaseView view) {
        mView= (Contact.View) view;
    }

    @Override
    public void unbindView() {
        dispose();
        mView= null;
    }

    @Override
    public void creatChildOrder(String token, String parentxh, String qyxh, final String jsondata) {
        ApiManager.getInstence().getJmmApi().createChildOrder(token,parentxh,qyxh,jsondata)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseData>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                      mView.showLoading();
                    }

                    @Override
                    public void onNext(BaseData jsonElement) {
                        if (TextUtils.equals(jsonElement.getStatus(),"0"))
                            mView.Successful(jsonElement.getMessage());
                        else
                            mView.Frailer(jsonElement.getMessage());
                    }

                    @Override
                    public void onError(Throwable e) {
                                mView.closeLoading();
                            mView.Frailer(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.closeLoading();
                    }
                });
    }
}
